#include "pch.h"
#include "ParticleEmitterFactory.h"
#include "ScriptPlugin.h"
#include "ResourceManager.h"
namespace mini
{
	CParticleEmitterFactory::CParticleEmitterFactory()
	{

	}

	CParticleEmitterFactory::~CParticleEmitterFactory()
	{

	}
	

	CParticleEmitterType* CParticleEmitterFactory::createEmitterFun(const std::string& szInfo)
	{
		try
		{
			lua_State* pLua = CScriptPlugin::getSinglePtr()->getLuaState();
			if (!CLuaBind::doFile(szInfo.c_str()))
			{
				throw "load file faile";
			}
			int iTop = lua_gettop(pLua);
			std::string szType = CResourceManager::getSinglePtr()->getTableItem<const char*>(pLua, "emitter", "typeName");
			CParticleEmitterType* pEmitter = nullptr;
			if (mEmits.find(szType) == mEmits.end())
			{
				if (szType == "waterfall")
				{
					pEmitter = new CParticleEmitterWaterFall();
					mEmits[szType] = pEmitter;
				}
			}
			else
			{
				mEmits[szType]->AddRef();
				pEmitter = mEmits[szType];
			}
			lua_settop(pLua, iTop);
			return pEmitter;
		}
		catch (const std::string szError)
		{
			MessageBoxA(nullptr, szError.c_str(), "error", 0);
			return nullptr;
		}
	}

}
